#!/usr/bin/python

try:
    import sys
    import urllib2
    import re
    from BeautifulSoup import BeautifulSoup
except ImportError,e:
    import sys
    print "Import Error : Libraries are missing.."
    sys.exit()

def usage():
    print "%s <search_item>" %(sys.argv[0])

def remove_html_tags(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

def formatSearchItem(item):
    item = re.sub(' +',' ',item)
    return item.replace(' ','+')

def getSesliSozluk(search_item):
    
    url= "http://m.seslisozluk.com/?word=%s" %(formatSearchItem(search_item))
    
    try: #get html from url
        response = urllib2.urlopen(url)
        data = response.read()
    except (BaseException):
        print '[!] Error Occured. '
        print '[?] Check whether system is Online.'
        return 99
    
    soup = BeautifulSoup(data)
    en2tr_result = soup.findAll('div',{'id':'dc_en_tr'})
    en2tr = remove_html_tags(str(en2tr_result))
    
    en2en_result = soup.findAll('div',{'id':'dc_en_en'})
    en2en = remove_html_tags(str(en2en_result))
    
    print "English to Turkish", en2tr.replace('[','').replace(']','')
    print "English to English", en2en.replace('[','').replace(']','')
    
    return

if __name__ == '__main__':
    
    # Check params
    if len(sys.argv) < 2 :
        usage()
        sys.exit(1)
        
    # Format search pattern
    search_pattern = str(sys.argv[1:]).replace('[\'','').replace('\']','').replace('\', \'',' ')
    
    # >>>>>>>>
    getSesliSozluk(search_pattern)